package com.xyht.medicine_management_system.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.user.entity.MmsApplication;
import com.xyht.medicine_management_system.modules.user.entity.req.MmsApplicationReq;
import com.xyht.medicine_management_system.modules.user.entity.resp.MmsApplicationResp;
import com.xyht.medicine_management_system.modules.user.mapper.MmsApplicationMapper;
import com.xyht.medicine_management_system.modules.user.service.MmsApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 应用列表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-12-18
 */
@Service
public class MmsApplicationServiceImpl extends ServiceImpl<MmsApplicationMapper, MmsApplication> implements MmsApplicationService {
    @Resource
    private MmsApplicationMapper mmsApplicationMapper;

    @Override
    public ResponseResult addApp(MmsApplicationReq mmsApplicationReq) {
        if (isNullOrEmpty(mmsApplicationReq.getAppClient(),
                mmsApplicationReq.getAppName(),mmsApplicationReq.getAppPath(),
                mmsApplicationReq.getUpDown())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsApplication addApplication = new MmsApplication();
        BeanUtils.copyProperties(mmsApplicationReq,addApplication);
        mmsApplicationMapper.insert(addApplication);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteApp(MmsApplicationReq mmsApplicationReq) {
        if (isNullOrEmpty(mmsApplicationReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        mmsApplicationMapper.deleteById(mmsApplicationReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateApp(MmsApplicationReq mmsApplicationReq) {
        if (isNullOrEmpty(mmsApplicationReq.getId(),mmsApplicationReq.getAppPath(),
                mmsApplicationReq.getAppClient(),mmsApplicationReq.getAppType())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsApplication mmsApplication = mmsApplicationMapper.selectById(mmsApplicationReq.getId());
        if (mmsApplication == null){
            return CommonResult.failed(CommonCodeEnum.ROLE_NOT_EXIST);
        }

        MmsApplication updateApplication = new MmsApplication();
        BeanUtils.copyProperties(mmsApplicationReq,updateApplication);
        mmsApplicationMapper.updateById(updateApplication);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getAppList(String searchStr, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<MmsApplication> queryWrapper = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(searchStr)){
            queryWrapper.and(wrapper->{
                wrapper.like(MmsApplication::getAppName,searchStr)
                        .or()
                        .like(MmsApplication::getAppType,searchStr)
                        .or()
                        .like(MmsApplication::getAppPath,searchStr);
            });
        }
        queryWrapper.orderByDesc(MmsApplication::getCreateTime);
        Page<MmsApplication> page = new Page<>(pageNum,pageSize);
        mmsApplicationMapper.selectPage(page,queryWrapper);
        List<MmsApplicationResp> list = page.getRecords()
                .stream()
                .map(mmsApplication -> {
                    MmsApplicationResp mmsApplicationResp = new MmsApplicationResp();
                    BeanUtils.copyProperties(mmsApplication,mmsApplicationResp);
                    return mmsApplicationResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult getAppDetail(String appId) {
        MmsApplication application = mmsApplicationMapper.selectById(appId);
        if (application == null){
            return CommonResult.failed(CommonCodeEnum.AUTH_APP_NOT_EXIST);
        }

        MmsApplicationResp mmsApplicationResp = new MmsApplicationResp();
        BeanUtils.copyProperties(application,mmsApplicationResp);
        return CommonResult.success(mmsApplicationResp);
    }
}
